package com.caac.core.web.action.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caac.core.config.QueryClass;
import com.caac.core.dm.Role;
import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction;
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;
import com.caac.core.web.form.RoleSearchForm;
import com.caac.core.web.view.RoleView;

import org.apache.struts.action.ActionMapping;

public class SearchRoleAction extends BasePageViewAction {
    private static final String QUERY_NAME = "roleQuery";
    private static final String ORDER_NAME = "roleOrder";
    private static final String VIEW_NAME = "roleView";

    protected PageViewForward doPageViewExecute(ActionMapping mapping,
                                                BaseForm form,
                                                HttpServletRequest request,
                                                HttpServletResponse response,
                                                int pageNo, int pageSize) throws
            java.lang.Exception {
        RoleSearchForm myForm = (RoleSearchForm) form;
        QueryClass qc = getQuery(QUERY_NAME);
        Page page = new Page(getBaseManager().getCount(Role.class, qc, myForm,
                myForm.isAnd()), pageNo, pageSize);
        List dmList = getBaseManager().getObjects(Role.class, qc, myForm,
                                                  myForm.isAnd(),
                                                  getOrder(ORDER_NAME), page);
        List vList = new ArrayList();
        for (int i = 0; i < dmList.size(); i++) {
            Role role = (Role) dmList.get(i);
            RoleView roleView = new RoleView();
            roleView.setRole(role);
            vList.add(roleView);
        }
        return new PageViewForward(forward(mapping), vList, page,
                                   getSelViewProps(VIEW_NAME), VIEW_NAME,
                                   ORDER_NAME);
    }
}
